//  封装的ajax
let debug = 0

function Ajax(domainName) {
  this.domainName = domainName
  this.loading = (title = 'loading', mask = true) => {
    // wx.showLoading({
    // mask,
    // title,
    // })
  }
  this.get = (_url, data = {}) => {
    this.loading()
    return new Promise((resolve, reject) => {
      let url = this.domainName + _url
      let MemberNo = wx.getStorageSync('MemberNo')
      if (MemberNo) {
        data.MemberNo = MemberNo || ''
      }
      url += (url.indexOf('?') < 0 ? '?' : '&') + param(data)
      let _this = this
      wx.request({
        url,
        method: 'GET',
        dataType: 'json',
        header: {
          "content-type": "application/json"
        },
        success(response) {
          if (debug) {
            failFunc(_this.domainName + url, null, response, 1)
          }
          // console.log(response.data)
          if (response.data.errcode == 1) {
            console.log(response.data)
          }
          resolve(response.data)
        },
        fail(err) {
          failFunc(_this.domainName + url, null, err)
        },
        complete() {
          wx.hideLoading()
        }
      })
    })
  }
  this.post = (url, data = {}) => {
    let MemberNo = wx.getStorageSync('MemberNo')
    if (MemberNo) {
      data = Object.assign(data, {
        MemberNo
      })
    }
    this.loading()
    return new Promise((resolve, reject) => {
      let _this = this
      wx.request({
        url: this.domainName + url,
        method: 'POST',
        data: data,
        dataType: 'json',
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success(response) {
          if (debug) {
            failFunc(_this.domainName + url, data, response, 1)
          }
          let res = response.data
          if (res.Errcode == 20000 && !data.isLsNoLogin) {
            alert('登录过期', '', false, '去登录').then(() => {
              wx.reLaunch({
                url: '/pages/index/index'
              })
            })
          } else {
            resolve(res)
          }
        },
        fail(err) {
          failFunc(_this.domainName + url, data, err)
        },
        complete() {
          wx.hideLoading()
        }
      })
    })
  }
}

function titleCase5(str) {
  if (!str) {
    return str
  }
  return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
}

function alert(content = 'content', title = 'title', showCancel = false, confirmText = 'confirm', cancelText = 'cancel') {
  content = titleCase5(content)
  title = titleCase5(title)
  confirmText = titleCase5(confirmText)
  cancelText = titleCase5(cancelText)
  return new Promise((resolve, reject) => {
    wx.showModal({
      title,
      content,
      showCancel,
      cancelText,
      confirmText,
      success(res) {
        if (res.confirm) {
          resolve()
        } else if (res.cancel) {
          reject()
        }
      }
    })
  })
}

function failFunc(url, data, err, act) {
  if (act != 1) {
    alert('System error, please try again-----')
  }
  console.warn(act == 1 ? '调试bug模式' : '数据请求出错,请截图给开发者,错误如下:')
  console.log('请求地址为：', url)
  console.log('请求数据为：', data)
  console.error('返回数据为：(请点开 data>data 截图)', err)
}

function param(data) {
  let url = ''
  for (var k in data) {
    let value = data[k] !== undefined ? data[k] : ''
    url += '&' + k + '=' + encodeURIComponent(value)
  }
  return url ? url.substring(1) : ''
}

module.exports = Ajax;